#-------------------------------------------------------------------------------
# Name:        CreateLanduseRaster
# Purpose:     Process to combine landuse, zoning, vacant lands, parks, and
#              transportation classes into one continuous raster surface.
# Author:      DASHNEY, JBURNS

#Notes:         Codes assigned to the landuse, zoning and transportation
#               values are arbitrary and have no meaning outside of this process.

# Created:     12/03/2014
#-------------------------------------------------------------------------------

import arcpy
import arcpy.da
import os
from stormwaterutilities import addMessage

temp = r"d:\temp\swsp_working.gdb"
if not arcpy.Exists(temp):
    arcpy.CreateFileGDB_management(r"d:\temp", "swsp_working.gdb")

arcpy.CheckOutExtension('Spatial')

#environmental settings
#extent_poly = os.path.join(temp, 'extent_poly') # r"\\oberon\stormwaterprogram\Data\GIS\StormwaterToolbox\Risk\WQ\Working\SWSP_working.gdb\RunoffCoeff_poly"
extent = r"\\ws18325\Stormwater D\RiskModel\Surface\LandClassification.gdb\RunoffCoefficient"

#arcpy.MinimumBoundingGeometry_management(extent, extent_poly, "ENVELOPE", "ALL")

arcpy.env.extent = arcpy.Raster(extent).extent
extent_poly = arcpy.Raster(extent).extent
#arcpy.env.mask = extent
arcpy.env.snapRaster = extent
arcpy.env.overwriteOutput = "True"

# Q - probably a better location for this connection file? - on ws18325?
egh_public = r"\\cassio\gis3\Arc_Misc\Tools\Scripts\egh_public on gisdb1.rose.portland.local.sde"

#vector inputs
addMessage("getting inputs")
taxlots = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.taxlots_pdx"
zoning = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.zoning_pdx"
vacant = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.vacant_land_pdx"
trans = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.streets_pdx"
unimproved = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.unimproved_row_pdx"
water = egh_public + r"\EGH_PUBLIC.ARCMAP_ADMIN.waterbodies_pdx"

#create subsets of inputs
#vacant and unimproved do not get subset
addMessage("making feature layer subsets")
taxlots_sub = arcpy.MakeFeatureLayer_management(taxlots, r"taxlots", "LANDUSE in ('VAC','REC','COM','CVCL','MULT','MFR','RES','SFR','IND')")
zoning_sub = arcpy.MakeFeatureLayer_management(zoning, r"zoning", "ZONE in ('OS','RF','R20','R10','R7','R5','R2.5,'R3','R2','R1','RH','RX','IR','CN1','CN2','C01','C02','CS','CM','CG','CX','EG1','EG2','EX','IG1','IG2','IH')")
local_st_sub = arcpy.MakeFeatureLayer_management(trans, r"local_st", "TYPE in ('1500','1521','1700','1740','1750','5500','5501')")
art_st_sub = arcpy.MakeFeatureLayer_management(trans, r"art_st", "TYPE in ('1221','1222','1223','1300','1400','1450','5301','5401')")
hwy_sub = arcpy.MakeFeatureLayer_management(trans, r"hwy", "TYPE in ('1110','1121','1122','1123','1200')")
water_sub = arcpy.MakeFeatureLayer_management(water, r"water", "MAJOR_WB = 'True' AND WB_TYPE = 'River'")

#clip inputs
addMessage("clipping inputs to extent")

taxlots_clip = arcpy.CopyFeatures_management(taxlots_sub, temp + r"\taxlots_clip")
zoning_clip = arcpy.CopyFeatures_management(zoning_sub, temp + r"\zoning_clip")
vacant_clip = arcpy.CopyFeatures_management(vacant, temp + r"\vacant_clip")
#local_clip = arcpy.Clip_analysis(local_st_sub, extent_poly,  temp + r"\local_clip")
#art_clip = arcpy.Clip_analysis(art_st_sub, extent_poly, temp + r"\art_clip")
#hwy_clip = arcpy.Clip_analysis(hwy_sub, extent_poly, temp + r"\hwy_clip")
unimproved_clip = arcpy.CopyFeatures_management(unimproved, temp + r"\unimproved_clip")
water_clip = arcpy.CopyFeatures_management(water_sub, temp + r"\water_clip")

addMessage("buffering streets")
unimproved_buff = arcpy.Buffer_analysis(unimproved, temp + r"\unimp_buff", 28, "FULL", "ROUND", "ALL")
local_buff = arcpy.Buffer_analysis(local_st_sub, temp + r"\local_buff", 28, "FULL", "ROUND", "ALL")
art_buff = arcpy.Buffer_analysis(art_st_sub, temp + r"\art_buff", 50, "FULL", "ROUND", "ALL")
hwy_buff = arcpy.Buffer_analysis(hwy_sub, temp + r"\hwy_buff", 80, "FULL", "ROUND", "ALL")

#add field to inputs
addMessage("adding CODE field")
arcpy.AddField_management(unimproved_buff, "CODE", "SHORT")
arcpy.AddField_management(local_buff, "CODE", "SHORT")
arcpy.AddField_management(art_buff, "CODE", "SHORT")
arcpy.AddField_management(hwy_buff, "CODE", "SHORT")
arcpy.AddField_management(taxlots_clip, "CODE", "SHORT")
arcpy.AddField_management(zoning_clip, "CODE", "SHORT")
arcpy.AddField_management(vacant_clip, "CODE", "SHORT")
arcpy.AddField_management(water_clip, "CODE", "SHORT")

#calculate int values that are associated with landuse
addMessage("calculating CODE field")
arcpy.CalculateField_management(vacant_clip, "CODE", 5, "PYTHON")
arcpy.CalculateField_management(unimproved_buff, "CODE", 6, "PYTHON")
arcpy.CalculateField_management(local_buff, "CODE", 7, "PYTHON")
arcpy.CalculateField_management(art_buff, "CODE", 8, "PYTHON")
arcpy.CalculateField_management(hwy_buff, "CODE", 9, "PYTHON")
arcpy.CalculateField_management(water_clip, "CODE", 10, "PYTHON")

#codes = assigned codes for landuse and zoning: RES:1, COM:2, IND:3, POS:4, VAC:5, TRU:6, TRL:7, TRM:8, TRH:9, WAT:10, UND:11
codes = {"RES":1,"RF":1,"R20":1,"R10":1,"R7":1,"R5":1,"R2.5":1,"R3":1,"R2":1,
    "R1":1,"RH":1,"RX":1,"IR":1,"MFR":1,"SFR":1,"COM":2,"CVCL":2,"MULT":2,
    "CN1":2,"CN2":2,"CO1":2,"CO2":2,"CS":2,"CM":2,"CG":2,"CX":2,"IND":3,"EG1":3,
    "EG2":3,"EX":3,"IG1":3,"IG2":3,"IH":3,"POS":4,"REC":4,"OS":4,"VAC":5}
cursor = arcpy.UpdateCursor(taxlots_clip)
for row in cursor:
    val = row.getValue("LANDUSE")
    if not codes.has_key(val):
        row.setValue("CODE", 11)
    else:
        row.setValue("CODE", codes[row.getValue("LANDUSE")])
    cursor.updateRow(row)
cursor = arcpy.UpdateCursor(zoning_clip)
for row in cursor:
    val = row.getValue("ZONE")
    if not codes.has_key(val):
        row.setValue("CODE", 11)
    else:
        row.setValue("CODE", codes[row.getValue("ZONE")])
    cursor.updateRow(row)

#remove undefined types (11) from taxlots- these cause issues
taxlots_temp = arcpy.MakeFeatureLayer_management(taxlots_clip)
arcpy.SelectLayerByAttribute_management(taxlots_temp, "NEW_SELECTION", "CODE=11")
arcpy.DeleteRows_management(taxlots_temp)

#convert vector inputs to raster
addMessage("converting vectors to raster")
taxlot_rast = arcpy.FeatureToRaster_conversion(taxlots_temp, "CODE", temp + r"\taxlot_rast", 3)
zoning_rast = arcpy.FeatureToRaster_conversion(zoning_clip, "CODE", temp + r"\zoning_rast", 3)
vacant_rast = arcpy.FeatureToRaster_conversion(vacant_clip, "CODE", temp + r"\vacant_rast", 3)
unimproved_rast = arcpy.FeatureToRaster_conversion(unimproved_buff, "CODE", temp + r"\unimproved_rast", 3)
local_rast = arcpy.FeatureToRaster_conversion(local_buff, "CODE", temp + r"\local_rast", 3)
art_rast = arcpy.FeatureToRaster_conversion(art_buff, "CODE", temp + r"\art_rast", 3)
hwy_rast = arcpy.FeatureToRaster_conversion(hwy_buff, "CODE", temp + r"\hwy_rast", 3)
water_rast = arcpy.FeatureToRaster_conversion(water_clip, "CODE", temp + r"\water_rast", 3)

addMessage("combining rasters")
arcpy.MosaicToNewRaster_management(r"d:\temp\swsp_working.gdb\hwy_rast;d:\temp\swsp_working.gdb\art_rast;d:\temp\swsp_working.gdb\local_rast;d:\temp\swsp_working.gdb\unimproved_rast;d:\temp\swsp_working.gdb\water_rast;d:\temp\swsp_working.gdb\vacant_rast;d:\temp\swsp_working.gdb\taxlot_rast;d:\temp\swsp_working.gdb\zoning_rast", temp, r"LanduseZoningHybrid", "", "4_BIT", "3", "1", "FIRST", "FIRST")
#arcpy.MosaicToNewRaster_management("C:\\Temp\\swsp_working.gdb\\hwy_rast;C:\\Temp\\swsp_working.gdb\\art_rast;C:\\Temp\\swsp_working.gdb\\local_rast;C:\\Temp\\swsp_working.gdb\\unimproved_rast;C:\\Temp\\swsp_working.gdb\\vacant_rast;C:\\Temp\\swsp_working.gdb\\water_rast;C:\\Temp\\swsp_working.gdb\\taxlot_rast;C:\\Temp\\swsp_working.gdb\\zoning_rast", temp, r"mosaic", "", "8_BIT_UNSIGNED", "3", "1", "FIRST", "FIRST")
#if this works changes output name to LanduseHybrid

#fill raster gaps
addMessage("filling raster gaps (Euclidean Allocation)")
output = r"\\ws18325\Stormwater D\RiskModel\Surface\\"

#Remove misclassified values from raster
raster_clean = arcpy.sa.SetNull(temp + r'\LanduseZoningHybrid', temp + r'\LanduseZoningHybrid', 'VALUE = 11')
raster_fill = arcpy.sa.EucAllocation(raster_clean)#, 500, "", 3, "CODE", output + 'LanduseZoningHybrid')
raster_fill.save(output + 'LanduseZoningHybrid.tif')

arcpy.AddField_management(output + r'\LanduseZoningHybrid.tif', 'Code', 'TEXT', '', '', 3)
arcpy.AddField_management(output + r'\LanduseZoningHybrid.tif', 'LongName', 'TEXT', '', '', 30)

fields = ['Value', 'Code', 'LongName']
with arcpy.da.UpdateCursor(output + 'LanduseZoningHybrid.tif', fields) as cursor:
    for row in cursor:
        if row[0] == 1:
            row[1] = 'RES'
            row[2] = 'Residential'
        elif row[0] == 2:
            row[1] = 'COM'
            row[2] = 'Commercial'
        elif row[0] == 3:
            row[1] = 'IND'
            row[2] = 'Industrial'
        elif row[0] == 4:
            row[1] = 'POS'
            row[2] = 'Parks and Open Space'
        elif row[0] == 5:
            row[1] = 'VAC'
            row[2] = 'Vacant Land'
        elif row[0] == 6:
            row[1] = 'TRU'
            row[2] = 'Unimproved ROW'
        elif row[0] == 7:
            row[1] = 'TRL'
            row[2] = 'Local Streets'
        elif row[0] == 8:
            row[1] = 'TRM'
            row[2] = 'Collector Streets'
        elif row[0] == 9:
            row[1] = 'TRH'
            row[2] = 'Arterials and Highways'
        elif row[0] == 10:
            row[1] = 'WAT'
            row[2] = 'Open Water'
        elif row[0] == 11: #This shold never happen, undefined values are removed and filled above
            row[1] = 'UND'
            row[2] = 'Undefined'
        cursor.updateRow(row)

addMessage("Process completed successfully - raster saved to: " + output)

